#include "comm.hpp"



static void getMessage(int fd)
{
    char buffer[SIZE] = {0};
    while(true)
    {
        memset(buffer,'\0',sizeof(buffer));
        int ret = read(fd, buffer, sizeof(buffer)-1);
        if(ret > 0)
        {
            cout <<"server: get a message from client:> " << buffer << endl;
        }
        else if(ret == 0)
        {
            cout <<"server: end of the file. quit!" << endl;
            break;
        }
        else{
            perror("read");
            break;
        }
    }
}

int main()
{
    //创建有名管道
    int ret = mkfifo(ipcPath.c_str(), MODE);
    assert(ret != -1);
    (void)ret;
    Log("server:create pipe success.\n",Debug);

    //打开管道
    int fd = open(ipcPath.c_str(),O_RDONLY);
    assert(fd != -1);
    Log("server: open pipe success.\n", Debug);

    //进行通信
    getMessage(fd);

    //关闭管道文件
    close(fd);
    Log("server: close pipe.\n",Debug);

    //删除管道文件
    unlink(ipcPath.c_str());
    Log("server: delete pipe.\n",Debug);

    return 0;
}